import config from './src/config/index'
import { formatBuildVersion } from './src/util/DateUtil'

export default {
  mode: 'spa',
  // mode: 'universal',
  /**
   * 相关配置
   */
  buildDir: '.dist',
  srcDir: 'src/',
  env: {
    BUILD_VERSION: formatBuildVersion(),
    RUNTIME: process.env.NODE_ENV
  },
  head: {
    title: config.website.name + ',' + config.website.desc,
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: config.website.desc || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ],
    script: [
      // 地图组件
      // { src: `https://webapi.amap.com/maps?v=1.4.11&key=${config.AMAP_JS_KEY}&plugin=AMap.PlaceSearch` },
      // { src: 'https://webapi.amap.com/ui/1.0/main.js?v=1.0.11' },
      // excel导出功能（注意此处的版本)，npm方式: { "xlsx": "^0.15.4", "file-saver": "^2.0.2"}
      { src: 'https://cdn.bootcss.com/xlsx/0.14.1/xlsx.full.min.js' },
      { src: 'https://cdn.bootcss.com/FileSaver.js/1.3.8/FileSaver.min.js' }
    ]
  },
  /*
  ** 自定义加载条
  */
  loading: { color: '#ff1a1a', height: '3px' },
  // 单页时白屏加载显示
  loadingIndicator: {
    name: '~/static/loading.html',
    color: '#3B8070',
    background: 'white'
  },
  css: [
    'normalize.css', 'animate.css',
    '~/assets/css/app.scss'
  ],
  plugins: [
    // { src: '~/plugins/nuxt-router', ssr: false }, 不推荐beforeEach方式: https://github.com/nuxt/nuxt.js/issues/3621
    { src: '~/plugins/vue-google-adsense', ssr: false },
    { src: '~/plugins/nuxt-error-handler', ssr: false },
    { src: '~/plugins/nuxt-persist', ssr: false },
    { src: '~/plugins/nuxt-directive', ssr: false },
    { src: '~/plugins/nuxt-axios', ssr: false },
    { src: '~/plugins/nuxt-loading', ssr: false },
    { src: '~/plugins/nuxt-iconfont', ssr: false },
    { src: '~/plugins/nuxt-echarts', ssr: false },
    { src: '~/plugins/nuxt-element-ui', ssr: false },
    { src: '~/plugins/nuxt-avue', ssr: false }
  ],
  router: {
    middleware: ['nuxt-router'],
    extendRoutes (routes, resolve) {
      // const profileIndex = routes.findIndex(route => route.name === 'profile')
      // routes[profileIndex] = {
      //   ...routes[profileIndex],
      //   components: {
      //     default: routes[profileIndex].component,
      //     notification: resolve(__dirname, 'components/mainTop.vue')
      //   },
      //   chunkNames: {
      //     top: 'components/mainTop'
      //   }
      // }
    }
  },
  modules: [
    ['@nuxtjs/axios', {
      progress: true,
      retry: false,
      credentials: true
    }],
    ['@nuxtjs/device'],
    ['@nuxtjs/sitemap', {
      hostname: config.website.url,
      path: 'sitemap_index.xml',
      gzip: false,
      exclude: []
    }]
    /** 网站SEO相关配置 **/
    // ['@nuxtjs/google-tag-manager', { id: config.googleTagManagerId }],
  ],
  buildModules: [
    // Doc: https://github.com/nuxt-community/eslint-module
    '@nuxtjs/eslint-module',
    // 自定义vue-router
    ['@nuxtjs/router', {
      fileName: 'router.js',
      keepDefaultRouter: true
    }]
  ],
  build: {
    // 默认: /_nuxt/, 生产环境配置此处cdn,参考: https://nuxtjs.org/api/configuration-build/#publicpath
    // CDN: nuxt build后复制对应的 /dist/client/目录到对应的cdn服务器上
    publicPath: config.serverUrls.CDN_BASE_URL,
    analyze: {
      analyzerMode: process.env.RUNTIME === 'development' ? 'static' : 'disabled'
    },
    babel: {
      presets: ['@nuxt/babel-preset-app'],
      'plugins': [
        [
          'component',
          {
            'libraryName': 'element-ui',
            'styleLibraryName': 'theme-chalk'
          }
        ]
      ],
      'comments': true
    },
    // 默认babel不会编译node-modules目录的文件,对于一些包采用源码形式打包的将不被转译
    transpile: [
      // element-ui引用
      /^element-ui/,
      // vue-echarts引用
      'vue-echarts', 'resize-detector'
    ],
    extend (config, ctx) {
      if (ctx.isDev) {
        config.devtool = ctx.isClient ? 'source-map' : 'inline-source-map'
      }
    }
  }
}
